package net.maku.propagate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.framework.security.user.SecurityUser;
import net.maku.propagate.convert.PropagateEducationConvert;
import net.maku.propagate.entity.PropagateEducationEntity;
import net.maku.propagate.query.PropagateEducationQuery;
import net.maku.propagate.vo.PropagateEducationVO;
import net.maku.propagate.dao.PropagateEducationDao;
import net.maku.propagate.service.PropagateEducationService;
import net.maku.system.service.SysOrgService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 宣传教育
 *
 * @author 阿沐 babamu@126.com
 * @since 1.0.0 2025-03-16
 */
@Service
@AllArgsConstructor
public class PropagateEducationServiceImpl extends BaseServiceImpl<PropagateEducationDao, PropagateEducationEntity> implements PropagateEducationService {

    private final SysOrgService sysOrgService;

    @Override
    public PageResult<PropagateEducationVO> page(PropagateEducationQuery query) {
        IPage<PropagateEducationEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));
        List<PropagateEducationVO> result = BeanUtil.copyToList(page.getRecords(), PropagateEducationVO.class);
        sysOrgService.setVisible(result);
        return new PageResult<>(result, page.getTotal());
    }

    private LambdaQueryWrapper<PropagateEducationEntity> getWrapper(PropagateEducationQuery query){
        LambdaQueryWrapper<PropagateEducationEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper.like(StringUtils.isNotEmpty(query.getTitle()),PropagateEducationEntity::getTitle,query.getTitle())
                .eq(ObjectUtils.isNotEmpty(query.getCourseClassificationl()),PropagateEducationEntity::getCourseClassification,query.getCourseClassificationl())
                .eq(ObjectUtils.isNotEmpty(query.getStatus()),PropagateEducationEntity::getStatus,query.getStatus())
                .orderByDesc(PropagateEducationEntity::getId);
    }

    @Override
    public void save(PropagateEducationVO vo) {
        PropagateEducationEntity entity = PropagateEducationConvert.INSTANCE.convert(vo);
        entity.setOrgId(SecurityUser.getUser().getOrgId());
        baseMapper.insert(entity);
    }

    @Override
    public void update(PropagateEducationVO vo) {
        PropagateEducationEntity entity = PropagateEducationConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    public void unmount(Long id, Integer status) {
        PropagateEducationEntity entity =new PropagateEducationEntity();
        entity.setId(id);
        entity.setStatus(status);
        baseMapper.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

}